package cn.iocoder.yudao.module.train.service.coursegroup;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.train.controller.admin.coursegroup.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.coursegroup.CourseGroupDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.train.dal.mysql.coursegroup.CourseGroupMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.*;

/**
 * 课程组管理 Service 实现类
 *
 * @author waynelam
 */
@Service
@Validated
public class CourseGroupServiceImpl implements CourseGroupService {

    @Resource
    private CourseGroupMapper courseGroupMapper;

    @Override
    public Integer createCourseGroup(CourseGroupSaveReqVO createReqVO) {
        // 插入
        CourseGroupDO courseGroup = BeanUtils.toBean(createReqVO, CourseGroupDO.class);
        courseGroupMapper.insert(courseGroup);

        // 返回
        return courseGroup.getId();
    }

    @Override
    public void updateCourseGroup(CourseGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateCourseGroupExists(updateReqVO.getId());
        // 更新
        CourseGroupDO updateObj = BeanUtils.toBean(updateReqVO, CourseGroupDO.class);
        courseGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteCourseGroup(Integer id) {
        // 校验存在
        validateCourseGroupExists(id);
        // 删除
        courseGroupMapper.deleteById(id);
    }

    @Override
        public void deleteCourseGroupListByIds(List<Integer> ids) {
        // 删除
        courseGroupMapper.deleteByIds(ids);
        }


    private void validateCourseGroupExists(Integer id) {
        if (courseGroupMapper.selectById(id) == null) {
            throw exception(COURSE_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public CourseGroupDO getCourseGroup(Integer id) {
        return courseGroupMapper.selectById(id);
    }

    @Override
    public PageResult<CourseGroupDO> getCourseGroupPage(CourseGroupPageReqVO pageReqVO) {
        return courseGroupMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CourseGroupDO> getCourseGroupList() {
        return courseGroupMapper.selectList();
    }


}
